package com.firebird.app.fekpsl.adapter.sword.rank

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import cn.walkpast.stamperlib.StampPadding
import cn.walkpast.stamperlib.StampType
import cn.walkpast.stamperlib.StampWatcher
import cn.walkpast.stamperlib.Stamper
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.bean.LevelScore
import com.firebird.app.fekpsl.utils.ImageLoader
import com.firebird.app.fekpsl.utils.MemberUtil

/**
 * Created by firebird on 2018/4/22.
 */
class LevelScoreRankAdapter(private val context: Context?, datas: MutableList<LevelScore>)
    : BaseQuickAdapter<LevelScore, BaseViewHolder>(R.layout.sword_rank_item_level_score_list) {

    override fun convert(helper: BaseViewHolder?, item: LevelScore?) {
        helper ?: return
        item ?: return

        val index=helper.layoutPosition

        var mStampWatcher1: StampWatcher = object : StampWatcher() {
            override fun onSuccess(bitmap: Bitmap?, requestId: Int) {
                super.onSuccess(bitmap, requestId)
                when (requestId) {
                    1001 ->                     //the result of text stamper
                    {
                        context?.let{helper.getView<ImageView>(R.id.iv_top)
                                .setImageBitmap(bitmap)
                        }}
                }
            }

            override fun onError(error: String?, requestId: Int) {
                super.onError(error, requestId)
                when (requestId) {
                    1001 -> Toast.makeText(context, "error:$error", Toast.LENGTH_SHORT).show()
                }
            }
        }

        val username = if (item.is_explicit==1) item.nice_name else item.real_name

        helper.setText(R.id.tv_user_name, username+"["+MemberUtil.getLevelName(item.level_score)+"]")
                .addOnClickListener(R.id.iv_avatar)
        val content = "积分:"+item.level_score.toString()
        helper.setText(R.id.tv_content, content)
        if (item.avatar_text.isNotEmpty()) {
            helper.getView<ImageView>(R.id.civ_avatar)
                    .visibility = View.VISIBLE
            context?.let {
                ImageLoader.load(it, item.avatar_text, helper.getView(R.id.civ_avatar))
            }
        } else {
            context?.let {
                helper.getView<ImageView>(R.id.civ_avatar)
                        .setImageDrawable(it.resources.getDrawable(R.drawable.icon_default_user))
            }
        }
        when(index){
            0->{
                context?.let{helper.getView<ImageView>(R.id.iv_top)
                    .setImageDrawable(it.resources.getDrawable(R.mipmap.top1))
                }}
            1->{
                context?.let{helper.getView<ImageView>(R.id.iv_top)
                        .setImageDrawable(it.resources.getDrawable(R.mipmap.top2))
                }}
            2->{
                context?.let{helper.getView<ImageView>(R.id.iv_top)
                    .setImageDrawable(it.resources.getDrawable(R.mipmap.top3))
            }}
            else->{
                var leftPos=0f
                when(index+1){
                    in 1..9->leftPos=3f
                    in 10..99->leftPos=4f
                    else->leftPos=6f
                }
                context?.let {
                    val bitmap = BitmapFactory.decodeResource(it.resources, R.mipmap.top0)
                    Stamper.with(it)
                            .setLabel((index+1).toString())
                            .setLabelColor(it.resources.getColor(R.color.common_white)) //Color.rgb(255, 60, 70)
                            .setLabelSize(40)
                            .setMasterBitmap(bitmap)
                            .setStampType(StampType.TEXT)
                            .setStampPadding(StampPadding(bitmap.width / leftPos, bitmap.height / 1.4f))
                            .setStampWatcher(mStampWatcher1)
                            .setRequestId(1001)
                            .build()
                }
             }
        }
        //helper.setText(R.id.tv_ranking,(index+1).toString())
    }
}
